package com.weekdo.moviecomic;

import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.util.Log;

public class Comic {
	private String comicEntry;
	private ArrayList<ArrayList<String>> lineList;
	
	public Comic(String comicEntry)
	{
		this.comicEntry = comicEntry;
	}
	
	public ArrayList<ArrayList<String>> getLineList()
	{
		if (this.lineList == null)
		{
			this.lineList = new ArrayList<ArrayList<String>>();
			
			FileReader r = null;
			int bufLength = 0;
			char[] buf = new char[10240];
			int contentLength = 0;
			String content = new String();

			try {
				r = new FileReader(FileManager.MCK_VIEW_CACHE_DIR+"/"+this.comicEntry+"/lines.txt");
				while ((bufLength = r.read(buf)) > 0)
				{
					contentLength += bufLength;
					content += new String(buf);
				}
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
			
			
			content = content.substring(0, contentLength).trim();
			if (content.length() == 0) return null;

			Log.d("!!CONTENT_LENGTH", ""+content.length());
			//Log.d("!!CONTENT", content);
			

			content = content.replace("\r", "");
			String[] fileLines = content.split("\n");
			for (int i=0; i<fileLines.length; i++)
			{
				//this.lineList.add(fileLines[i]);
				String[] tempArray = fileLines[i].split("\\|", 2);
				//this.lineList[i] = new ArrayList<String>();tempArray
				ArrayList<String> tempArrayList = new ArrayList<String>();
				tempArrayList.add(tempArray[0]);
				tempArrayList.add(tempArray[1]);
				this.lineList.add(tempArrayList);
			}
		}

		return lineList;
	}

}
